package com.supercube.sport.yoga.controller;

import com.github.pagehelper.PageInfo;
import com.supercube.common.controller.BaseController;
import com.supercube.common.utils.StringUtils;
import com.supercube.sport.yoga.domain.SiteInfoDTO;
import com.supercube.sport.yoga.service.SiteInfoService;
import com.supercube.sport.yoga.service.VenueInfoService;
import com.supercube.sport.yoga.utils.ResultInfo;
import com.supercube.sport.yoga.vo.SiteInfoVO;
import com.supercube.sport.yoga.vo.VenueInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/siteInfo")
@Api(tags = "场地管理")
public class SiteInfoController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SiteInfoService siteInfoService;

    @Autowired
    private VenueInfoService venueInfoService;


    @ApiOperation(value = "保存场地基本信息", notes = "保存场地基本信息")
    @PostMapping("/save")
    public ResultInfo saveSiteInfo(@RequestBody SiteInfoDTO dto) {
        VenueInfoVO venueInfoVO = venueInfoService.queryVenueInfoById(dto.getVenueId());
        if (venueInfoVO == null) {
            return ResultInfo.failed("保存失败，场地关联的场馆不存在");
        }
        dto.setId(randomUUID());
        siteInfoService.save(dto);
        return ResultInfo.success("保存成功");
    }

    @ApiOperation(value = "查询场地信息列表，根据场馆id", notes = "查询场地信息列表，根据场馆id")
    @GetMapping("/querySiteInfoListByVenueId")
    public ResultInfo querySiteInfoListByVenueId(@RequestParam("id") String id, String usePurpose) {
        if (StringUtils.isEmpty(id)) {
            return ResultInfo.validateFailed();
        }
        List<SiteInfoVO> list = siteInfoService.querySiteInfoByVenueId(id, usePurpose);
        PageInfo<SiteInfoVO> pageInfo = new PageInfo<>(list);
        return ResultInfo.success(pageInfo);
    }

    @ApiOperation(value = "查询场地信息列表不分页，根据场馆id", notes = "查询场地信息列表不分页，根据场馆id;" +
            "usePurpose场馆用途，1：团课，2：私教教室，3：团课+私教，4：=1+3，5：=2+3")
    @GetMapping("/querySiteInfoListByVenueIdNotPage")
    public ResultInfo querySiteInfoListByVenueIdNotPage(@RequestParam("id") String id, String usePurpose) {
        if (StringUtils.isEmpty(id)) {
            return ResultInfo.validateFailed();
        }
        List<SiteInfoVO> list = siteInfoService.querySiteInfoByVenueId(id, usePurpose);
        return ResultInfo.success(list);
    }

    @ApiOperation(value = "修改场地基本信息", notes = "修改场地基本信息")
    @PostMapping("/update")
    public ResultInfo updateSiteInfo(@RequestBody SiteInfoDTO dto) {
        siteInfoService.update(dto);
        return ResultInfo.success("修改成功");
    }

    @ApiOperation(value = "删除场地基本信息", notes = "删除场地基本信息")
    @GetMapping("/delete")
    public ResultInfo deleteSiteInfoById(@RequestParam("id") String id) {
        if (isEmpty(id)) {
            return ResultInfo.failed("删除失败，id不能为空");
        }
        siteInfoService.deleteVenueInfoById(id);
        return ResultInfo.success("删除成功");
    }

    @ApiOperation(value = "批量删除场地基本信息", notes = "批量删除场地基本信息")
    @GetMapping("/batchDeleteSiteInfoByIds")
    public ResultInfo batchDeleteSiteInfoByIds(@RequestParam("ids") String[] ids) {
        if (ids == null) {
            return ResultInfo.failed("删除失败，id不能为空");
        }
        List<String> success = new ArrayList<>();
        for (String id : ids) {
            if (siteInfoService.deleteVenueInfoById(id) > 0) {
                success.add(id);
            }
        }
        String message = "";
        try {
            message = "删除成功数:" + success.size() + ",删除失败数：" + (ids.length - success.size());
        } catch (Exception e) {
            logger.error("统计有误！" + e);
        }
        return ResultInfo.success("删除完成", message);
    }

    @ApiOperation(value = "根据id查询场地基本信息", notes = "根据id查询场地基本信息")
    @PostMapping("/querySiteInfoById")
    public ResultInfo querySiteInfoById(@RequestParam("id") String id) {
        SiteInfoVO siteInfoDTO = siteInfoService.querySiteInfoById(id);
        return ResultInfo.success(siteInfoDTO);
    }

}
